package com.syh.app.basic.views;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.syh.app.basic.R;
import com.syh.app.basic.utils.AppUtil;
import com.syh.app.basic.utils.Tooast;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

import static com.syh.app.basic.config.BasicConstants.COUNT_DOWN;

/**
 * 创建者：SYH
 * 时间：2020/4/24 17:16
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class GetVerificationCodeDialog extends Dialog {
    /* verCheckType  验证码检测类型 */
    public static final int VER_CODE_TYPE_PHONE = 1;//手机验证码
    public static final int VER_CODE_TYPE_EMAIL = 0;//邮箱验证码
    public static final int VER_CODE_TYPE_GOOGLE = 2;//谷歌验证码

    private TextView tvAccount;
    private TextView tvVerCodeType;
    private TextView tvGetVerCode;
    private Button btnConfirm;
    private EditText etCode;

    private Context mContext;
    private Object tag = null;
    private int mCheckType;
    private Disposable mDisposable;
    private boolean isTimerWaitting = false;

    private WindowManager.LayoutParams mLayoutParams;
    private OnClickListener mOnClickListener;

    public GetVerificationCodeDialog(Context context) {
        super(context, R.style.FullDialog);
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_get_verification_code, (ViewGroup) null);
        dialogView.findViewById(R.id.tvCancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GetVerificationCodeDialog.this.cancel();
            }
        });
        tvAccount = dialogView.findViewById(R.id.tvAccount);
        tvVerCodeType = dialogView.findViewById(R.id.tvVerCodeType);
        tvGetVerCode = dialogView.findViewById(R.id.tvGetVerCode);
        btnConfirm = dialogView.findViewById(R.id.btnConfirm);
        etCode = dialogView.findViewById(R.id.etCode);
        this.setContentView(dialogView);
        tvGetVerCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickListener != null && !isTimerWaitting) {
                    mOnClickListener.onGetVerCode(tag);
                    isTimerWaitting = true;
                }
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickListener != null) {
                    String code = etCode.getText().toString();
                    if (TextUtils.isEmpty(code)) {
                        Tooast.normalInfo(mContext.getString(R.string.basic_input_ver_code));
                    } else {
                        GetVerificationCodeDialog.this.cancel();
                        mOnClickListener.onConfirm(mCheckType, code);
                    }
                }
            }
        });
    }

    public void timerStart() {
        if (isTimerWaitting) {
            Observable.interval(0, 1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Long>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            isTimerWaitting = false;
                            mDisposable = d;
                            tvGetVerCode.setEnabled(false);
                        }

                        @Override
                        public void onNext(Long aLong) {
                            if (aLong <= 300L) {
                                tvGetVerCode.setText((COUNT_DOWN - aLong) + "s");
                            } else {
                                tvGetVerCode.setText(R.string.basic_send_sms_ver_code);
                                tvGetVerCode.setEnabled(true);
                                if (!mDisposable.isDisposed()) {
                                    mDisposable.dispose();
                                    mDisposable = null;
                                }
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (!mDisposable.isDisposed()) {
                                mDisposable.dispose();
                                mDisposable = null;
                            }
                        }

                        @Override
                        public void onComplete() {
                            if (!mDisposable.isDisposed()) {
                                mDisposable.dispose();
                                mDisposable = null;
                            }
                        }
                    });
        }
    }

    public void getVerCodeFailed(){
        isTimerWaitting = false;
    }

    public void build(String account, int checkType, Object tag, OnClickListener onClickListener) {
        this.tag = tag;
        build(account, checkType, onClickListener);
    }

    public void build(String account, int checkType, OnClickListener onClickListener) {
        this.mCheckType = checkType;
        isTimerWaitting = false;
        tvAccount.setText(account);
        this.mOnClickListener = onClickListener;
        String typeName = "";
        if (checkType == GetVerificationCodeDialog.VER_CODE_TYPE_PHONE) {
            typeName = mContext.getString(R.string.basic_phone_ver_code);
        } else if (checkType == GetVerificationCodeDialog.VER_CODE_TYPE_EMAIL) {
            typeName = mContext.getString(R.string.basic_email_ver_code);
        } else if (checkType == GetVerificationCodeDialog.VER_CODE_TYPE_GOOGLE) {
            typeName = mContext.getString(R.string.basic_google_ver_code);
        }
        tvVerCodeType.setText(typeName);
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
            mDisposable = null;
            tvGetVerCode.setText(R.string.basic_send_sms_ver_code);
            tvGetVerCode.setEnabled(true);
        }
    }

    public interface OnClickListener {
        void onGetVerCode(Object tag);

        void onConfirm(int checkType, String code);
    }

}
